<?php

/*
 * C活动功能模块
 * 功能：活动
 */

namespace User\Controller;

use Think\C_Controller;

class EvaluateController extends C_Controller {
        CONST READ_STATUS = 2;  // 已读
        CONST UNREAD_STATUS = 1; //未读


    public function __construct() {
        parent::__construct();
        $this->store_id = (int) $_GET['store_id'];
        if ($this->store_id < 1) {
            throw new \Exception();
        }
        $s = new \Home\Logic\StoreLogic();
        $store = $s->getStore($this->store_id);
        $this->assign('logo', $store['logo']);
        $this->assign('store_name', $store['name']);
        $this->assign('store',$store);
        $this->e = new \Home\Logic\EvaluateLogic();
        
    }

/*
  待评价商品 
*/
    public function index(){
        layout(false);
        $item_id = (int)$_GET['item_id'];
        $items = new \Home\Logic\ShopitemLogic(); 
        $item = $items->getItemById($item_id, $this->store_id,true);
        $client_id = $_SESSION['client_id'];
        $order_number = $_GET['order_number'];
        $this->assign('item',$item);
        $this->assign('client_id',$client_id);
        $this->assign('order_number',$order_number);
        $this->display('index');

    }

/*
    评价商品 提交

*/
    public function submit(){
        $data['store_id'] = (int)$_GET['store_id'];
        $data['item_id']  = (int)$_GET['item_id'];
        $data['client_id'] = (int)$_GET['client_id'];
        $data['item_number'] = trim($_GET['item_number']);
        $data['order_number'] = trim($_GET['order_number']);
        $data['content'] = $_GET['content'];
        $result = $this->e->getEvaluateByOrdernumber($data['store_id'],$data['order_number'],$data['client_id'],$data['item_id']);
        if($data['client_id']<1){
            redirect("/user/passport/login?store_id=".$this->store_id);
        } 
        if($data['item_id']<1 || strlen($data['item_number'])<1 || $result){
            exit('false');
        }
        $arr['total'] = (int)$_GET['total'];
        $arr['environment'] = (int)$_GET['environment'];
        $arr['barber'] = (int)$_GET['barber'];
        $data['star_level'] = json_encode($arr);
        $data['status'] = self::UNREAD_STATUS;
        $data['create_time'] = time();
        $data['b_user_id']  = 0;
        $data['evaluate_id']  = 0;
        $re = $this->e->insertEvaluate($data);
        if($re){
            $order = new \Home\Logic\OrderLogic();
            $status = array('status'=>2);
            $res = $order->updateOrder($data['order_number'],$status);
            exit('true');
        }else{
            exit('false');
        }
        exit;  
    }

/*
    未读评价    

*/
    public function unread(){
        layout('layout2');
        $store_id = (int)$_GET['store_id'];
        $client_id = (int)$_SESSION['client_id'];
        $start = (int)$_GET['start'];
        $items = new \Home\Logic\ShopitemLogic(); 
        $evaluates = $this->e->unRead($store_id,$client_id,$start);
        foreach($evaluates as $k=>$v){
            $evaluates[$k]['item'] = $items->getItemById($v['item_id'], $this->store_id,true);
        }

        if($start==0){  // 第一加载
            $this->assign('evaluates',$evaluates);    
            $this->display('unread');
        }else{
            if(count($evaluates)>0){
                $evaluates = json_encode($evaluates);
                echo $evaluates;
                exit;
            }else{
                exit('false');
            }
        }
        
    }

/*
    更多评价

*/   
    public function more_evaluate(){
         layout(false);
         $item_number = trim($_GET['item_number']);
         $start = (int)$_GET['start'];   
       
        //更多评价 
        $evaluates = $this->e->getMoreEvaluate($item_number,$this->store_id,$start);
        //用户信息
        $u = new \Home\Logic\UserLogic();
        foreach($evaluates as $kk => $vv){
            $evaluates[$kk]['user'] = $u->getUserById($vv['client_id']);
        }

        if($start==0){ //第一次加载  
            //商品item_number
            $it = new \Home\Logic\ShopitemLogic();
            $item = $it->getItemByNumber($item_number,$this->store_id);
            //商品评分
            $score = $this->e->score($item['item_number'],$this->store_id);
            foreach($score as $k=>$v){
                if($v['star_level']=='0'){
                    unset($score[$k]);
                }
                $arr = json_decode($v['star_level'],true);
                $sc += ($arr['total']+$arr['environment']+$arr['barber'])/3;
            }
            $score = substr($sc/(count($score)),0,3);
            $this->assign('score',$score);
            $this->assign('evaluates',$evaluates);
            $this->assign('item',$item);
            $this->display('more_evaluate');
        }else{
            if(count($evaluates)<1){
                exit('false');
            }
            exit(json_encode($evaluates));
        }
    } 



}